/*
 * Copyright (c) 2010-2020 IISI.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of IISI.
 */
package com.iisigroup.log.analysis.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 *
 */
public class Utils {
    //================================================
    //== [Enumeration types] Block Start
    //== [Enumeration types] Block End 
    //================================================
    //== [static variables] Block Start
    private static final int BUFFER = 1024;

    //== [static variables] Block Stop 
    //================================================
    //== [instance variables] Block Start
    //== [instance variables] Block Stop 
    //================================================
    //== [static Constructor] Block Start
    //== [static Constructor] Block Stop 
    //================================================
    //== [Constructors] Block Start (含init method)
    //== [Constructors] Block Stop 
    //================================================
    //== [Static Method] Block Start
    /** 
     * 數據解壓縮 
     *  
     * @param is 
     * @param os 
     * @throws Exception 
     */
    public static void decompress(InputStream is, OutputStream os) throws Exception {
        final GZIPInputStream gis = new GZIPInputStream(is);
        int count;
        byte data[] = new byte[BUFFER];
        while ((count = gis.read(data, 0, BUFFER)) != -1) {
            os.write(data, 0, count);
        }

        gis.close();
    }

    public static void compressGzipFile(String file, String gzipFile) {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        GZIPOutputStream gzipOS = null;
        try {
            fis = new FileInputStream(file);
            fos = new FileOutputStream(gzipFile);
            gzipOS = new GZIPOutputStream(fos);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                gzipOS.write(buffer, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //close resources
            try {
                if (gzipOS != null)
                    gzipOS.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // LOGGER.error("", e);
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // LOGGER.error("", e);
            }
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // LOGGER.error("", e);
            }
        }
    }

    public static void outputGzipFile(final String content, String gzipFile) {
        FileOutputStream fos = null;
        GZIPOutputStream gzipOS = null;
        try {
            final byte[] srcBytes = content.getBytes();
            fos = new FileOutputStream(gzipFile);
            gzipOS = new GZIPOutputStream(fos);
            gzipOS.write(srcBytes);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //close resources
            try {
                if (gzipOS != null)
                    gzipOS.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // LOGGER.error("", e);
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // LOGGER.error("", e);
            }
        }
    }
    //== [Static Method] Block Stop 
    //================================================
    //== [Accessor] Block Start
    //== [Accessor] Block Stop 
    //================================================
    //== [Overrided Method] Block Start (Ex. toString/equals+hashCode)
    //== [Overrided Method] Block Stop 
    //================================================
    //== [Method] Block Start
    //####################################################################
    //## [Method] sub-block : 
    //####################################################################    
    //== [Method] Block Stop 
    //================================================
    //== [Inner Class] Block Start
    //== [Inner Class] Block Stop 
    //================================================
}
